package cn.sumpu.app.guoguojie.dao.impl;

import java.util.List;
import java.util.Map;

import org.mybatis.spring.support.SqlSessionDaoSupport;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import cn.sumpu.app.guoguojie.dao.FriendDao;
import cn.sumpu.app.guoguojie.domain.Friend;
import cn.sumpu.app.guoguojie.domain.FriendGroup;
import cn.sumpu.app.guoguojie.domain.Group;
import cn.sumpu.app.guoguojie.domain.Ext.FriendExt;
import cn.sumpu.app.guoguojie.domain.Ext.FriendFamily;
import cn.sumpu.app.guoguojie.exception.AppBusinessException;

public class FriendDaoImpl extends SqlSessionDaoSupport implements FriendDao {

	private final static Logger logger = LoggerFactory
			.getLogger(FriendDaoImpl.class);

	
	@Override
	public FriendFamily searchFriendEmail(Map<String,Object> map)
			throws AppBusinessException {
		try {
			return (FriendFamily) getSqlSession().selectOne("cn.sumpu.app.guoguojie.persistence.FriendMapper.searchFriendEmail",map);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("searchFriendEmail failure.", e);
			}
			throw new AppBusinessException("searchFriendEmail failure.", e);
		}
	}

	@Override
	public FriendFamily searchFriendDoorplate(Map<String,Object> map)
			throws AppBusinessException {
		try {
			return (FriendFamily) getSqlSession().selectOne("cn.sumpu.app.guoguojie.persistence.FriendMapper.searchFriendDoorplate",map);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("searchFriendDoorplate failure.", e);
			}
			throw new AppBusinessException("searchFriendDoorplate failure.", e);
		}
	}
	
	//根据标签名称查找好友
	@SuppressWarnings("unchecked")
	@Override
	public List<FriendFamily> searchFriendByTag(Map<String,Object> map)
			throws AppBusinessException {
		try {
			return getSqlSession().selectList("cn.sumpu.app.guoguojie.persistence.FriendMapper.searchFriendByTag",map);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("searchFriendByTag failure.", e);
			}
			throw new AppBusinessException("searchFriendByTag failure.", e);
		}
	}
	
	//根据标签查找的总记录数
	@Override
	public Integer searchFriendByTagCount(Map<String, Object> map)
			throws AppBusinessException {
		try {
			Object obj = getSqlSession().selectOne("cn.sumpu.app.guoguojie.persistence.FriendMapper.searchFriendByTagCount",map);
			return (Integer) (obj==null ? 0:obj);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("searchFriendByTagCount failure.", e);
			}
			throw new AppBusinessException("searchFriendByTagCount failure.", e);
		}
	}


	@SuppressWarnings("unchecked")
	@Override
	public List<FriendFamily> searchFriendCondition(Map<String, Object> map)
			throws AppBusinessException {
		try {
			return  getSqlSession().selectList("cn.sumpu.app.guoguojie.persistence.FriendMapper.searchFriendCondition",map);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("searchFriendCondition failure.", e);
			}
			throw new AppBusinessException("searchFriendCondition failure.", e);
		}
	}
	
	
	@Override
	public int searchFriendConditionCount(Map<String, Object> map)
			throws AppBusinessException {
		try {
			Object obj = getSqlSession().selectOne("cn.sumpu.app.guoguojie.persistence.FriendMapper.searchFriendConditionCount",map);
			return (Integer) (obj==null ? 0:obj);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("searchFriendConditionCount failure.", e);
			}
			throw new AppBusinessException("searchFriendConditionCount failure.", e);
		}
	}



	@Override
	public void insertFriend(Friend friend) throws AppBusinessException {
		try {
			getSqlSession().insert("cn.sumpu.app.guoguojie.persistence.FriendMapper.insertFriend",friend);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("insertFriend failure.", e);
			}
			throw new AppBusinessException("insertFriend failure.", e);
		}
	}

	@Override
	public void deleteFriend(Map<String, Object> map)
			throws AppBusinessException {
		try {
			getSqlSession().delete("cn.sumpu.app.guoguojie.persistence.FriendMapper.deleteFriend",map);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("deleteFriend failure.", e);
			}
			throw new AppBusinessException("deleteFriend failure.", e);
		}
	}

	@Override
	public void insertGroup(Group group) throws AppBusinessException {
		try {
			getSqlSession().insert("cn.sumpu.app.guoguojie.persistence.FriendMapper.insertGroup",group);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("insertGroup failure.", e);
			}
			throw new AppBusinessException("insertGroup failure.", e);
		}
	}

	@Override
	public void updateGroup(Group group) throws AppBusinessException {
		try {
			getSqlSession().update("cn.sumpu.app.guoguojie.persistence.FriendMapper.updateGroup",group);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("updateGroup failure.", e);
			}
			throw new AppBusinessException("updateGroup failure.", e);
		}
	}

	@Override
	public void deleteGroup(String identifier) throws AppBusinessException {
		try {
			getSqlSession().delete("cn.sumpu.app.guoguojie.persistence.FriendMapper.deleteGroup",identifier);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("deleteGroup failure.", e);
			}
			throw new AppBusinessException("deleteGroup failure.", e);
		}
	}

	@Override
	public void insertFriendGroup(FriendGroup friendGroup)
			throws AppBusinessException {
		try {
			getSqlSession().insert("cn.sumpu.app.guoguojie.persistence.FriendMapper.insertFriendGroup",friendGroup);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("insertFriendGroup failure.", e);
			}
			throw new AppBusinessException("insertFriendGroup failure.", e);
		}
	}
	
	

	@Override
	public void updateFriendGroup(Map<String, Object> map)
			throws AppBusinessException {
		try {
			getSqlSession().insert("cn.sumpu.app.guoguojie.persistence.FriendMapper.updateFriendGroup",map);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("updateFriendGroup failure.", e);
			}
			throw new AppBusinessException("updateFriendGroup failure.", e);
		}
	}


	@SuppressWarnings("unchecked")
	@Override
	public List<Group> getGroupList(Map<String,Object> map) throws AppBusinessException {
		try {
			return getSqlSession().selectList("cn.sumpu.app.guoguojie.persistence.FriendMapper.getGroupList",map);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("getGroupList failure.", e);
			}
			throw new AppBusinessException("getGroupList failure.", e);
		}
	}



	@Override
	public void agreeFriend(Map<String, Object> map)
			throws AppBusinessException {
		try {
			getSqlSession().update("cn.sumpu.app.guoguojie.persistence.FriendMapper.agreeFriend",map);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("agreeFriend failure.", e);
			}
			throw new AppBusinessException("agreeFriend failure.", e);
		}
	}

	

	@Override
	public void updateFriend(Map<String, Object> map)
			throws AppBusinessException {
		try {
			getSqlSession().update("cn.sumpu.app.guoguojie.persistence.FriendMapper.updateFriend",map);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("updateFriend failure.", e);
			}
			throw new AppBusinessException("updateFriend failure.", e);
		}
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<FriendFamily> maybeFriends(Map<String, Object> map)
			throws AppBusinessException {
		try {
			return getSqlSession().selectList("cn.sumpu.app.guoguojie.persistence.FriendMapper.maybeFriends",map);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("maybeFriends failure.", e);
			}
			throw new AppBusinessException("maybeFriends failure.", e);
		}
	}



	@Override
	public int maybeFriendsCount(Map<String, Object> map)
			throws AppBusinessException {
		try {
			Object obj = getSqlSession().selectOne("cn.sumpu.app.guoguojie.persistence.FriendMapper.maybeFriendsCount",map);
			return (Integer) (obj==null ? 0:obj);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("maybeFriendsCount failure.", e);
			}
			throw new AppBusinessException("maybeFriendsCount failure.", e);
		}
	}



	

	


	@SuppressWarnings("unchecked")
	@Override
	public List<FriendFamily> getMyFriendsFamily(Map<String, Object> map)
			throws AppBusinessException {
		try {
			return getSqlSession().selectList("cn.sumpu.app.guoguojie.persistence.FriendMapper.getMyFriendsFamily",map);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("getMyFriendsFamily failure.", e);
			}
			throw new AppBusinessException("getMyFriendsFamily failure.", e);
		}
	}



	@Override
	public int getMyFriendsFamilyCount(Map<String, Object> map)
			throws AppBusinessException {
		try {
			Object obj = getSqlSession().selectOne("cn.sumpu.app.guoguojie.persistence.FriendMapper.getMyFriendsFamilyCount",map);
			return (Integer) (obj==null ? 0:obj);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("getMyFriendsFamilyCount failure.", e);
			}
			throw new AppBusinessException("getMyFriendsFamilyCount failure.", e);
		}
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<FriendExt> getFamilyMember(Map<String, Object> map)
			throws AppBusinessException {
		try {
			return getSqlSession().selectList("cn.sumpu.app.guoguojie.persistence.FriendMapper.getFamilyMember",map);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("getFamilyMember failure.", e);
			}
			throw new AppBusinessException("getFamilyMember failure.", e);
		}
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Friend> getFriend(Map<String, Object> map)
			throws AppBusinessException {
		try {
			return getSqlSession().selectList("cn.sumpu.app.guoguojie.persistence.FriendMapper.getFriend",map);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("getFriend failure.", e);
			}
			throw new AppBusinessException("getFriend failure.", e);
		}
	}

	@Override
	public void updateFriendGroupDef(Map<String, Object> map)
			throws AppBusinessException {
		try {
			getSqlSession().update("cn.sumpu.app.guoguojie.persistence.FriendMapper.updateFriendGroupDef",map);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("updateFriendGroupDef failure.", e);
			}
			throw new AppBusinessException("updateFriendGroupDef failure.", e);
		}
	}

	@Override
	public void deleteFriendGroup(Map<String,Object> map)
			throws AppBusinessException {
		try {
			getSqlSession().delete("cn.sumpu.app.guoguojie.persistence.FriendMapper.deleteFriendGroup",map);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("deleteFriendGroup failure.", e);
			}
			throw new AppBusinessException("deleteFriendGroup failure.", e);
		}
	}

	/**
	 * 查看俩家是否是好友
	 * @param myDoorplate		自己家的门牌号
	 * @param friendDoorplate	好友家的门牌号
	 * @return					0（不是好友）或1（是好友）
	 * @throws AppBusinessException
	 */
	@Override
	public Integer getFriendByFamily(Map<String, Object> map)
			throws AppBusinessException {
		try {
			return (Integer) getSqlSession().selectOne("cn.sumpu.app.guoguojie.persistence.FriendMapper.getFriendByFamily",map);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("getFriendByFamily failure.", e);
			}
			throw new AppBusinessException("getFriendByFamily failure.", e);
		}
	}	

}
